package com.net.framework.help.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.framework.help.R;
import com.net.framework.help.progress.ProgressCancelListener;
import com.net.framework.help.utils.StringUtil;

/**
 * @author zhangxiaobin
 * @version V1.0
 * @ClassName: CustomProgressDialog.java
 * @email 154722566@qq.com
 * @Date 2016年2月25日 下午4:09:10
 * @Description: TODO
 */
public class LoadAnimProgressDialog extends Dialog {
    private static LoadAnimProgressDialog customProgressDialog = null;
    private static Context oldContext;
    private static String loadMessage;

    public LoadAnimProgressDialog(Context context) {
        super(context);
    }

    public LoadAnimProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    private static LoadAnimProgressDialog createDialog(Context context) {
        customProgressDialog = new LoadAnimProgressDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.layout_customprogressdialog);
//        customProgressDialog = new LoadAnimProgressDialog(context, R.style.CustomProgressDialog);
//        customProgressDialog.setContentView(R.layout.layout_loding_progressdialog);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        return customProgressDialog;
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        if (customProgressDialog == null) {
            return;
        }
//        ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
//        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
//        animationDrawable.start();
    }


    public static void setMsg(String message) {
        loadMessage = message;
    }

    public static void showProgressDialog(Context context) {
        Activity activity = getActivity(context);
        if (null == activity) {
            return;
        }
        if (activity.isFinishing()) {
            return;
        }
        if (null != oldContext) {
            if (context != oldContext) {
                customProgressDialog = LoadAnimProgressDialog.createDialog(context);
                customProgressDialog.setCanceledOnTouchOutside(false);
            }
        }
        if (customProgressDialog == null) {
            customProgressDialog = LoadAnimProgressDialog.createDialog(context);
            customProgressDialog.setCanceledOnTouchOutside(false);
        }
        if (!customProgressDialog.isShowing()) {
            customProgressDialog.show();
        }
        oldContext = context;
    }

    public static void showProgressDialog(Context context, final ProgressCancelListener mProgressCancelListener, boolean cancelable) {
        Activity activity = getActivity(context);
        if (null == activity) {
            return;
        }
        if (activity.isFinishing()) {
            return;
        }

        if (null != oldContext) {
            if (context != oldContext) {
                customProgressDialog = LoadAnimProgressDialog.createDialog(context);
                customProgressDialog.setCanceledOnTouchOutside(false);
            }
        }

        if (customProgressDialog == null) {
            customProgressDialog = LoadAnimProgressDialog.createDialog(context);
            customProgressDialog.setCanceledOnTouchOutside(false);
            if (cancelable) {
                customProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        mProgressCancelListener.onCancelProgress();
                    }
                });
            }
        }

        if (!StringUtil.isBlank(loadMessage)) {
            TextView tv_msg = customProgressDialog.findViewById(R.id.tv_msg);
            tv_msg.setVisibility(View.VISIBLE);
            tv_msg.setText(loadMessage);
        }
        if (!customProgressDialog.isShowing()) {
            customProgressDialog.show();
        }
        oldContext = context;
    }

    public static void dismissProgressDialog() {
        if (customProgressDialog != null && customProgressDialog.isShowing()) {
            Activity activity = getActivity(customProgressDialog.getContext());
            if (null == activity) {
                customProgressDialog = null;
                return;
            }
            if (activity.isFinishing()) {
                customProgressDialog = null;
                return;
            }
            customProgressDialog.dismiss();
            customProgressDialog = null;
        }
    }

    private static Activity getActivity(Context context) {
        while (!(context instanceof Activity) && context instanceof ContextWrapper) {
            context = ((ContextWrapper) context).getBaseContext();
        }
        if (context instanceof Activity) {
            return (Activity) context;
        } else
            return null;
    }
}